<?php

function getLanguage() {
	$fLang = __DEFAULT_LANGUAGE__;
	
	if (isset($_GET["lang"]) && ($_GET["lang"] != "")) {
		$fLang = $_GET["lang"];
	};
	
	return $fLang;
}

function getCurrentPage() {
	$fCurr = __DEFAULT_PAGE__;
	
	if (isset($_GET["p"]) && ($_GET["p"] != "")) {
		$fCurr = $_GET["p"];
	};
	
	return $fCurr;
}

function getDatabaseConnection() {
	$con = mysql_connect(__DB_HOST__, __DB_USER__, __DB_PASSWORD__);
	if (!$con)
  	{
		die('Could not connect: ' . mysql_error());
	}
	
	$db_selected = mysql_select_db(__DB_DATABASE__, $con);
	if (!$db_selected)
	{
		die ('Can\'t use ' . __DB_DATABASE__ . ': ' . mysql_error());
	}
	
	return $con;
}

function getContent($description) {
	$connection = getDatabaseConnection();

	$sql = "SELECT " . lang . " FROM site_fields WHERE description = '" . $description . "'";
	$result = mysql_query($sql, $connection);
	
	mysql_close($connection);
	
	if (!$result)
	{
		die("No content found or server error.");
	}
	
	$arr = mysql_fetch_array($result);
	$content = $arr[lang];
	
	return $content;
}

function getDirContent($directory)
{
	$files = array();
	if ( $handle = @opendir( $directory ) )
    {
		while ( false !== ( $file = readdir( $handle ) ) )
		{
			if ( $file != ".." && $file != "." )
			{
				$files[] = $file;
			}
		}
    } else {
    	echo "Can\'t read directory!<br/>";
    }
    
    if ($files) {
    	return $files;
    }
}

function uploadFile($filename, $fileTemp, $path, $lang, $description)
{
	move_uploaded_file($fileTemp, $path . $filename);
	
	
	$con = getDatabaseConnection();
	$sql = "UPDATE site_fields SET " . $lang . " = '". $filename . "' WHERE description='$description'";
	$result = mysql_query($sql, $con);
	if ($result)
	{
		echo "File \"" . $filename . "\" uploaded successfully!<br/>";
	} else {
		echo "The file was not uploaded successfully!<br/>";
	}
	
	mysql_close($con);
}

?>